<template>
  <el-dialog
    title="提示"
    :visible.sync="show"
    :width="width"
    :top="top"
    :before-close="handleClose"
  >
    <el-form
      label-position="top"
      :model="infoForm"
      :rules="rules"
      ref="infoForm"
      label-width="100px"
    >
      <el-upload
        action="/api/students/uploadStuAvatar"
        list-type="picture-card"
        name="headimgurl"
        :limit="1"
        :on-preview="handlePictureCardPreview"
        :on-success="appendInfo"
        :on-remove="handleRemove"
      >
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="" />
      </el-dialog>
      <el-form-item label="姓名" prop="name">
        <el-input :disabled="disable" v-model="infoForm.name"></el-input>
      </el-form-item>

      <el-form-item label="项目地址" prop="productUrl">
        <el-input :disabled="disable" v-model="infoForm.productUrl"></el-input>
      </el-form-item>

      <el-form-item label="班级" prop="class">
        <el-input :disabled="disable" v-model="infoForm.class"></el-input>
      </el-form-item>

      <el-form-item label="年龄" prop="age">
        <el-input :disabled="disable" v-model="infoForm.age"></el-input>
      </el-form-item>

      <el-form-item label="城市" prop="city">
        <el-input :disabled="disable" v-model="infoForm.city"></el-input>
      </el-form-item>

      <el-form-item label="学历" prop="degree">
        <el-input :disabled="disable" v-model="infoForm.degree"></el-input>
      </el-form-item>

      <el-form-item label="描述" prop="description">
        <el-input :disabled="disable" v-model="infoForm.description"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :disabled="disable" @click="resetForm('infoForm')"
        >取 消</el-button
      >
      <el-button
        :disabled="disable"
        type="primary"
        @click="submitForm('infoForm')"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: "custom-dialog",
  props: {
    width: {
      type: String,
      default: "600px",
    },
    top: {
      type: String,
      default: "5%",
    },
    show: {
      type: Boolean,
      default: false,
    },
    disable: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      infoForm: {
        name: "",
        class: "",
        age: "",
        city: "",
        degree: "",
        productUrl: "",
        description: "",
        headimgurl: "",
      },
      rules: {
        name: [{ required: true }],
        class: [{ required: true }],
        age: [{ required: true }],
        city: [{ required: true }],
        degree: [{ required: true }],
        productUrl: [{ required: true }],
        description: [{ required: true }],
      },
      dialogImageUrl: "",
      dialogVisible: false,
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit("datasubmit", this.infoForm);
          setTimeout(() => {
            this.resetForm("infoForm");
          }, 500);
        } else {
          this.$message({
            message: "字段不完整",
            duration: 800,
            type: "error",
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleClose() {
      this.$emit("handleclose");
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    appendInfo(file) {
      this.infoForm.headimgurl = file.headimgurl;
    },
  },
};
</script>

<style scoped>
.custom-dialog {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: #fff;
  box-shadow: 0 0 15px #ccc;
  padding: 5px 15px;
  overflow: auto;
}
.custom-dialog-wrap {
}
>>> .el-form--label-top .el-form-item__label {
  float: left;
}
</style>